package server.business;

import java.util.TimerTask;

import server.NetworkService;
import server.domain.Auction;

/**
 * This class can be passed to a timer; it removes an auction from the list of
 * active auctions and sends a notification to the owner and the highest bidder.
 * 
 * @author Lukas
 *
 */
public class AuctionStopper extends TimerTask {
	private final AuctionManager manager;
	private final NetworkService service;
	private final int id;
	
	/**
	 * @param manager The manager with the auction in it's list.
	 * @param service The service that provides the ability to send user notifications
	 * @param id The id of the auction that shall be stopped.
	 */
	public AuctionStopper(AuctionManager manager, NetworkService service, int id) {
		this.manager = manager;
		this.id = id;
		this.service = service;
	}

	@Override
	public void run() {
		Auction a = manager.removeAuction(id);
		if (a == null) {
			System.err.println("Auction with id " + id + " could not be removed: not found!");
			return;
		}
		String message = "!auction-ended " + a.getHighestBidder() + " " + manager.convertPrice(a.getBid())
				+ " " + a.getDescription();
		service.sendMessage(a.getHighestBidder(), message);
		service.sendMessage(a.getOwner(), message);
	}

}
